﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eVoteClient.util;
using System.Security.Cryptography;

namespace eVoteClient
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            global.socket = new Socket_io(global.destIP, global.destPort);
            var socket = global.socket;

            //Code for hashing
            string pw_enc = Encryption.sh1(passwordTxt.Text);
            
            //now have the hash
            string message = "LGN_REQ#" + emailTxt.Text + "#" + pw_enc;
            socket.writer.WriteLine(message);
            socket.writer.Flush();
            
            string reply = socket.reader.ReadLine();

            //MessageBox.Show(reply);

            string code = reply.Split('#')[1]; //code for Access granted or not
            string type = reply.Split('#')[2]; //type of user Admin or not
            
            if(code.Equals("0"))
                MessageBox.Show("Wrong email or password");
            else if (code.Equals("2"))
                MessageBox.Show("Your account is still not activate, please activate your account before you login.");
            else
            {
                if (type.Equals("1"))
                    global.isAdmin = true;
                global.Email = emailTxt.Text;
                new Home().Show();
                this.Hide();
            }

            
            
            
            
        }

        private static void ThreadProcHome()
        {
            Application.Run(new Home());
        }

        private void label3_Click(object sender, EventArgs e)
        {
            new Register().Show();
            this.Hide();
        }

        public static void ThreadProc()
        {
            Application.Run(new Register());
        }

        private void Login_Load(object sender, EventArgs e)
        {
            this.Location = new Point(350, 200);
            this.Size = new Size(572, 324);
        }

        private void activate_click(object sender, EventArgs e)
        {
            new ActivateAccount().Show();
            this.Hide();  
        }

        

        public static void ThreadProc2()
        {
            Application.Run(new ActivateAccount());
        }

    }
}
